%% THE portfolio management system
% Calculates and visualizes various portfolio analytics and graphs
%% Rights and disclaimer
% Author: Benjamin J. J. Voigt (bvoigt@gmail.com)
% Published: Oct 2012
% Version: 0.0.1
% License: BSD 2-Clause, free to use and copy retaining original author reference
% 
% Disclaimer: USE AT YOUR OWN RISK; NO INVESTMENT RECOMMENDATION IMPLIED;
% NOT ASSOCIATED WITH PAST, CURRENT OR FUTURE EMPLOYER OR ASSOCIATES OF THE
% AUTHOR; DO NOT USE THIS CODE TO INVEST YOUR OWN MONEY OR TO 
% ENCOURAGE OTHERS TO INVEST MONEY;
%% Required toolboxes
% Financial Toolbox, Data Feed Toolbox, Statistics
% Toolbox
%% Purpose and notes
% Purpose: main script to initialize and start the analysis.
%
% Notes: 
% This initial version contains hard coded values and references to replicate the
% exact example data set and formulas described in BKN: Z. Bodie, A. Kane, A. J. Marcus 
% INVESTMENTS,9th Edition, 2012, McGraw-Hill, NewYork
% The use of adjusted prices may distort the statistics slightly compared
% to the ones published in the book.
%
% Known limitations:
% 1)The index date array is used to align individual stocks, actual trade
% dates may NOT match. The reason is data problems in some sources
% regarding dates and the low importance of dates for the statistical
% calculations performed by the function
% 2)Annualization from monthly to 1 year yields estimates for a 1 year
% investment horizont and holding period only
% 3) Use of adjusted prices from Yahoo! without checking and validation
% 4) Works for equity asset clas only at this time
%

% Global, function wide variables
g_symbol_cell = {'HPQ', 'DELL', 'WMT', 'TGT', 'BP', 'RDS-B'};
g_symbol_alpha_estimates_mat = (zeros(1, length(g_symbol_cell)));
g_rfr = 0.06;
g_index_cell = {'^GSPC'};
g_price_fints = (zeros(61,length(g_symbol_cell)));
g_return_fints = (zeros(61,length(g_symbol_cell)));
g_reg_results = (zeros(length(g_symbol_cell),19));
g_symbol_riks_para = (zeros(length(g_symbol_cell),4));
g_index_risk_para = (zeros(1,4));
g_analysis_start_date = 'May 01 01'; %'May 01 01';
g_analysis_end_date = 'May 01 06';%'May 01 06';

% Local, dummy, loop or sub-section specific variables
l_data_cell = (zeros(61,2));

% Load index data for 60 months, monthly adjusted close starting May 2001
l_data_cell = LoadData('datafeed_full', g_index_cell(1), 'Adj Close', g_analysis_start_date,g_analysis_end_date, 'm');
g_price_fints = fints(l_data_cell(1:end,1), l_data_cell(1:end,2), strrep(g_index_cell(1), '^', ''));
g_return_fints = tick2ret(g_price_fints.GSPC);

% Load stock data 60 months, monthly adjusted close starting May 2001
% The index date array is used to align individual stocks, actual trade
% dates may NOT match, yahoo price vector is fliped and attached to index
% dates
for i=1:length(g_symbol_cell)
    l_data_cell = LoadData('datafeed_full', g_symbol_cell(i), 'Adj Close', g_analysis_start_date,g_analysis_end_date, 'm');
    g_price_fints = [g_price_fints (fints(g_price_fints.dates, flipud(l_data_cell(1:end,2)), strrep(cell2mat(g_symbol_cell(i)), '-', '')))];
    g_return_fints = [g_return_fints tick2ret(g_price_fints.(strrep(cell2mat(g_symbol_cell(i)),'-','')))];
end

% Visualize returns data
plot(g_return_fints);

% Estimate alphas
% TODO: Original alphas from BKN [0.015 -0.01 -0.005 0.0075 0.012 0.0025];
% TODO: Analyst alphas from BKN  [0.1471 0.1753 0.1932 0.2814 0.1797 0.0357];
g_symbol_alpha_estimates_mat = [0.1471 0.1753 0.1932 0.2814 0.1797 0.0357];
% AlphaEstimates(g_symbol_cell, ...
%     fts2mat([g_price_fints(end:end).HPQ, ...
%     g_price_fints(end:end).DELL, g_price_fints(end:end).WMT, ...
%     g_price_fints(end:end).TGT, g_price_fints(end:end).BP, ...
%     g_price_fints(end:end).RDSB]));

% Calculate optimal TB weights
% TreynorBlack(...)
[g_symbol_optimal_weights g_active_port_alpha g_active_port_beta...
    g_active_port_pct_of_total g_passive_port_pct_of_total i1 i2 i3 i4 i5 i6 i7]...
= TreynorBlack(g_price_fints, g_return_fints, g_symbol_cell, g_index_cell, ...
     g_rfr, g_symbol_alpha_estimates_mat, {''}, 'm');

% Fomulate output
fprintf('Pct to active: %8.4f\n',g_active_port_pct_of_total*100);
fprintf('Pct to passive: %8.4f\n',(1-g_active_port_pct_of_total)*100);
fprintf('Beta of active: %8.4f\n',g_active_port_beta);
for i=1:length(g_symbol_cell)
   fprintf('Pct invested in symbol %4.4s: %8.4f\n',cell2mat(g_symbol_cell(i)),g_symbol_optimal_weights(i)*100); 
end